<?php
defined('BASEPATH') OR exit('No direct script access allowed');

/**
* 
*/
class Auth_model extends MY_model {
	private $T = 'auth';

	public function get_lists($where = [], $select = [], $order_by = 'parent_id ASC, sort ASC, id ASC') {
		return $this->db
			->select($select)
			->order_by($order_by)
			->get_where($this->T, $where)
			->result_array();
	}

	/**
	 * [insert 添加数据]
	 * @return [int] [description]
	 */
	public function insert() {
		$data = $this->get_data();
		if (empty($data)) {
			$this->error = '没有数据';
			return 0;
		}

		$this->db->insert($this->T, $data);
		$insert_id = $this->db->insert_id();

		if (empty($insert_id)) {
			$this->error = '新增失败';
			return 0;
		}
		$this->error = '';

		return $insert_id;
	}

	public function get_one($where = [], $select = []) {
		if (empty($where)) {
			return [];
		}
		return $this->db
			->select($select)
			->get_where($this->T, $where)
			->row_array();
	}

	public function update($where = [], $data = []) {
		if (empty($where)) {
			return 0;
		}
		
		empty($data) AND $data = $this->get_data();
		if (empty($data)) {
			$this->error = '没有数据';
			return 0;
		}
		$this->db->update($this->T, $data, $where);
		return $this->db->affected_rows();
	}
	
	/**
	 * 删除数据
	 * @param  array
	 * @return [int]
	 */
	public function delete($where = []) {
		if (empty($where)) {
			return 0;
		}

		$this->db->delete($this->T, $where);
		return $this->db->affected_rows();
	}
}